package com.sino.tb.auditing.entity;

import java.sql.*;
import java.util.*;

public class DeclareTypeDetail {
	private String key;
	private String name;
	private DeclareItem item;

	public static DeclareTypeDetail find(String key, Connection conn) {
		DeclareTypeDetail dtd = null;
		String sql = "SELECT    t1.*, " +
					 "t2.c_code, t2.c_name, t2.c_type, t2.c_list_creater, t2.c_bid, t2.c_tablename " +
					 "FROM      t_st_declare_detail t1, " +
					 "          t_st_proj t2 " +
					 "WHERE     t1.c_seq = ? AND " +
					 "t1.c_item_seq = t2.c_seq";

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, key);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				dtd = new DeclareTypeDetail();
				dtd.setKey(rs.getString("c_seq"));
				dtd.setName(rs.getString("c_item_name"));
				DeclareItem di = new DeclareItem(rs.getString("c_item_seq"));
				di.setCode(rs.getString("c_code"));
				di.setName(rs.getString("c_item_name"));
				di.setType(rs.getString("c_type"));
				di.setListString(rs.getString("c_list_creater"));
				di.setTableID(rs.getString("c_bid"));
				di.setTableName(rs.getString("c_tablename"));
				dtd.setItem(di);
			}
			rs.close();
			pstmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dtd;

	}

	public static DeclareTypeDetail[] findAll(String key, Connection conn) {
		DeclareTypeDetail[] result = null;
		ArrayList middle = new ArrayList();
		String sql = "SELECT    t1.*, " +
					 "t2.c_code, t2.c_name, t2.c_type, t2.c_list_creater, t2.c_bid, t2.c_tablename " +
					 "FROM      t_st_declare_detail t1, " +
					 "          t_st_proj t2 " +
					 "WHERE     t1.c_type_seq = ? AND " +
					 "t1.c_item_seq = t2.c_seq";

		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, key);
			ResultSet rs = pstmt.executeQuery();
			int i = 0;
			while (rs.next()) {
				DeclareTypeDetail dtd = new DeclareTypeDetail();
				dtd.setKey(rs.getString("c_seq"));
				dtd.setName(rs.getString("c_name"));
				DeclareItem di = new DeclareItem(rs.getString("c_item_seq"));
				di.setCode(rs.getString("c_code"));
				di.setName(rs.getString("c_name"));
				di.setType(rs.getString("c_type"));
				di.setListString(rs.getString("c_list_creater"));
				di.setTableID(rs.getString("c_bid"));
				di.setTableName(rs.getString("c_tablename"));
				dtd.setItem(di);
				middle.add(i++, dtd);
			}
			rs.close();
			pstmt.close();
			ListIterator lit = middle.listIterator();
			result = new DeclareTypeDetail[middle.size()];
			i = 0;
			while (lit.hasNext()) {
				result[i++] = (DeclareTypeDetail) lit.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;

	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public DeclareItem getItem() {
		return item;
	}

	public void setItem(DeclareItem item) {
		this.item = item;
	}

}
